package Thread.New.线程安全;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@SuppressWarnings("all")
public class LockRunableImpl implements Runnable{

    private int num = 100;
    //创建一个锁
    Lock l = new ReentrantLock();
    @Override
    public void run() {
        while(true)
        {
            l.lock();
            //放个锁
            if (num > 0) {
                try {
                    Thread.sleep(10);
                    System.out.println(Thread.currentThread().getName() + "-->" + num);
                    num--;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    l.unlock();
                    //施放锁
                    //这样无论程序是否出现异常都会把锁施放
                }
            }
        }
    }
}
